package Communication;

/**
 * 线程通信
 */
public class communication {
    public static void main(String[] args) {
        Number number = new Number();
        Thread thread1 = new Thread(number);
        Thread thread2 = new Thread(number);
        thread1.setName("线程1");
        thread2.setName("线程2");
        thread1.start();
        thread2.start();
    }
}
class Number implements Runnable{
    private static int number=1;

    @Override
    public void run() {
        synchronized (this) {

            while (true){
                notifyAll();


                if (number<=100){
                    System.out.println(Thread.currentThread().getName()+":"+number);
                    number++;
                    try {
                        wait();  //线程阻塞，并且会自动释放锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    break;
                }
            }
        }
    }
}
